#!/usr/bin/python
# -*- coding: UTF-8 -*-
from SMTP import settings


class Manager():
    def __init__(self, server: str):
        """
        初始化...检测提供商
        :param server:
        """
        servers = []
        print(settings.SMTP)
        for value in settings.SMTP:
            servers.append(value)
        if server in servers:
            self.server = server
        else:
            raise Exception('错误的邮箱服务商：' + server + '  支持的邮件服务商列表：' + ''.join(servers))

    def send_mail(self, subject: str, content: str, receiver_email: str, receiver_nickname: str):
        """
        发送邮件
        :param subject:
        :param content:
        :param receiver_email:
        :param receiver_nickname:
        :return:
        """
        server = self.__getattr__()
        return server.send_mail(subject, content, receiver_email, receiver_nickname)

    def __getattr__(self):
        """
        自动加载SMTP模块
        :rtype: ServersInterface
        """
        module = __import__('Instagram.SMTP.Servers.' + self.server, None, None, ('Instagram.SMTP.Servers'))
        return getattr(module, self.server)()

#
# if (__name__ == '__main__'):
#     manager = Manager(settings.TARGET_SERVER)
#     print(manager.send_mail('Test', '123', '1714255949@qq.com', 'Aiweline'))
